import pdfkit
from tb_forms.models import *
from django.conf import settings
import datetime,time
import requests


def get_num(site,tmpl_id,id_form):
    try:
        site_code= TbSite.objects.filter(site_name=site).values('site_code')[0]['site_code']
    except:
        site_code=site.site_code
    if site_code in ['LH','GL','S','深圳']:
        site_code ='S'
    elif site_code in ['J','晉城']:
        site_code= 'J'
    elif site_code in ['Z','鄭州']:
        site_code= 'Z'
    elif site_code in ['C', '成都']:
        site_code ='C'

    a = TbFormtype.objects.filter(id=tmpl_id).values('type_code','type_id')[0]
    form_id = list(TbForms.objects.filter(status=1, table_type_id=tmpl_id).values('id')).index({'id': int(id_form)})
    function_code = a['type_code']
    type_id =str( float((form_id+1)/100)).replace('.',"")

    if TbApplyform.objects.filter(is_form=1).count()== 0:
        last_num = '000000000000000'
    else:
        last_num = TbApplyform.objects.filter(is_form=1).values('card_id').order_by('-id')[0]['card_id']

    today = datetime.datetime.now().strftime('%y%m%d')
    if last_num[6:12] != today:

        date_flow = today+'0001'
    else:
        if int(last_num[-4:]) ==9999:
            return False
        else:
            date_flow = str(int(last_num[6:]) + 1)
    card_id=function_code+site_code+type_id+date_flow

    return card_id


def get_pdf(html_path,pdf_path,message,pdf_mode,data_url):
    try:
        url2 = settings.MEDIA_ROOT + 'html/css/'
        url3 = settings.MEDIA_ROOT + 'html/js/'
        mid = """
            <link rel="stylesheet" type="text/css" href="%stable.css">
            <script type="text/javascript" src="%sjquery-2.2.3.min.js"></script>
            <script type="text/javascript" src="%sjquery.qrcode.js"></script>
            <script type="text/javascript" src="%sqrcode.js"></script>
            <script type="text/javascript" src="%sfunction.js"></script>
        """ % (url2, url3, url3, url3, url3)
        msn = """<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>"""+mid+"""</head>
    <body>
    <div class="form-header">
    <div id="form_qrcode" data-url="%s">
    </div>
    </div>
    """ % data_url
        footer = """<br><br><br><br><br><br><br><br><br><br></body></html>"""
        all_mess = msn+message+footer
        with open(html_path,'w+',encoding="utf-8") as f:
            f.write(all_mess)
        if pdf_mode == "vertical":
            options = {'page-size': 'A4'}
            pdfkit.from_url(html_path, pdf_path, options=options)
            return True
        options = {'page-width': '297', 'page-height': '210'}
        pdfkit.from_url(html_path, pdf_path, options=options)
        return True
    except Exception as e:
        return str(e)

from django.conf import settings
def post_msn(data):
    try:
        requests.get('http://'+settings.MQTT+'/?topic=st/message&qos=2&message='+data)
        return 1
    except Exception as e:
        return 0


# if __name__ == '__main__':
    # msd = message
    # get_pdf("E:/delpdf/123sad.html","E:/delpdf/dskf.pdf",message)
